package com.jams.goodox.activity.serve.headhunter;

import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.jams.goodox.BaseActivity;
import com.jams.goodox.R;
import com.jams.goodox.SePref;
import com.jams.goodox.constant.AppConfig;
import com.jams.goodox.constant.H5UrlConstants;
import com.jams.goodox.entity.ChooseItem;
import com.jams.goodox.entity.JobBean;
import com.jams.goodox.entity.resp.UserJobDetailResp;
import com.jams.goodox.net.RetrofitCallbackListener;
import com.jams.goodox.net.services.HeadHunterService;
import com.jams.goodox.utils.CollectionUtils;
import com.jams.goodox.utils.DataCacheManager;
import com.jams.goodox.utils.StringUtils;
import com.jams.goodox.utils.StyleUtil;
import com.jams.library.fresco.FrescoUtil;

import java.util.HashMap;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;
import retrofit2.Call;
import uama.share.ShareView;

/**
 * 人人都是猎头 职位详情
 * <p>
 * create by 顾佳佳 on 2017/05/18
 */
public class JobInfoDetailActivity extends BaseActivity {

    public static final String ID = "ID";
    @Bind(R.id.tv_job_name)
    TextView tvJobName;
    @Bind(R.id.tv_job_money)
    TextView tvJobMoney;
    @Bind(R.id.tv_job_year)
    TextView tvJobYear;
    @Bind(R.id.tv_job_education)
    TextView tvJobEducation;
    @Bind(R.id.sdv_company)
    SimpleDraweeView sdvCompany;
    @Bind(R.id.tv_company_name)
    TextView tvCompanyName;
    @Bind(R.id.tv_company_info)
    TextView tvCompanyInfo;
    @Bind(R.id.tv_company_address)
    TextView tvCompanyAddress;
    @Bind(R.id.tv_money)
    TextView tvMoney;
    @Bind(R.id.tv_money_type)
    TextView tvMoneyType;
    @Bind(R.id.tv_job_info_dec)
    TextView tvJobInfoDec;
    @Bind(R.id.lv_job_call)
    LinearLayout lvJobCall;
    @Bind(R.id.rl_job_detail)
    RelativeLayout rlJobDetail;
    @Bind(R.id.tv_welfare)
    TextView tvWelfare;
    @Bind(R.id.share_view)
    ShareView shareView;
    private JobBean jobBean;
    private String positionId;
    private List<ChooseItem> yearList, educationList, priseList, levelList, personList;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_job_info_detail;
    }

    @Override
    protected void init() {
        StyleUtil.titleBackKey(this, "职位详情");
        positionId = getIntent().getStringExtra(ID);

        yearList = CollectionUtils.arrayToList(getResources().getStringArray(R.array.year_need), -1);
        educationList = CollectionUtils.arrayToList(getResources().getStringArray(R.array.education_need), -1);
        priseList = CollectionUtils.arrayToList(getResources().getStringArray(R.array.prise_need), -1);
        levelList = CollectionUtils.arrayToList(getResources().getStringArray(R.array.company_stage), -1);
        personList = CollectionUtils.arrayToList(getResources().getStringArray(R.array.company_persons), -1);
        if (positionId != null) {
            getJobDetail();
        }
    }

    //获取职位详情
    private void getJobDetail() {
        showLoading();
        enqueue(createCallService(HeadHunterService.class).getUserJObDetail(positionId), new RetrofitCallbackListener<UserJobDetailResp>() {
            @Override
            public void onSuccess(Call<UserJobDetailResp> call, UserJobDetailResp result) {
                misLoading();
                jobBean = result.getData();
                if (jobBean != null) {
                    setData();
                    rlJobDetail.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onError(String msg) {
                misLoading();
            }
        });
    }

    //set data
    private void setData() {
        tvJobName.setText(jobBean.getJobName());
        tvJobMoney.setText(priseList.get(jobBean.getSalary()).getName());
        tvJobYear.setText(yearList.get(jobBean.getJobLifeRequire()).getName());
        tvJobEducation.setText(educationList.get(jobBean.getEducationRequire()).getName());
        String treatment = "";
        if (CollectionUtils.hasData(jobBean.getTreatment())) {
            for (int i = 0; i < jobBean.getTreatment().size(); i++) {
                if (i == jobBean.getTreatment().size() - 1) {
                    treatment = treatment + jobBean.getTreatment().get(i).getName();
                } else {
                    treatment = treatment + jobBean.getTreatment().get(i).getName() + ",";
                }
            }
        }
        if(TextUtils.isEmpty(treatment)){
            tvWelfare.setVisibility(View.GONE);
        }else {
            tvWelfare.setVisibility(View.VISIBLE);
            tvWelfare.setText("职位诱惑：" + treatment);
        }
        FrescoUtil.loadNetUrl(sdvCompany, jobBean.getEnterpriseLogo());
        tvCompanyName.setText(jobBean.getEnterpriseName());
        tvCompanyInfo.setText(jobBean.getIndustry() + "/" + levelList.get(jobBean.getLevel()).getName() + "/" + personList.get(jobBean.getCount()).getName());
        tvCompanyAddress.setText(jobBean.getAddress());
        tvMoney.setText(StringUtils.doubleToIntegerString(jobBean.getReward()) + "元");
        tvJobInfoDec.setText(jobBean.getJobDescrip());
    }

    @OnClick(R.id.lv_job_call)
    public void onViewClicked() {
        HashMap<String, String> urlParams = new HashMap<>();
        urlParams.put("token", SePref.getToken());
        urlParams.put("mobileType", AppConfig.MOBILE_TYPE);
        urlParams.put("recommendId", DataCacheManager.getInstance().getUser().getUserId());
        urlParams.put("id", jobBean.getId());
//        String sign = SignBuilder.encryptToSHA(SignBuilder.getSign(urlParams));
//        urlParams.put("sign",sign);
        shareView.setDefaultLinkWithMsg("这里有一份适合你的工作哦","请按照提示填写在线简历，并发送详细简历至公司邮箱", StringUtils.getUrl(H5UrlConstants.JobDetail, urlParams),  R.mipmap.ic_launcher);
        shareView.setVisibility(View.VISIBLE);
    }
}
